<?php
namespace Kuoqi;

/**
 * Class Session
 * @package Kuoqi
 * User kuoqi
 * Datetime 2022/2/28 23:01
 * Remark session
 */
class Session{
	
	static protected $domain;
	static protected $sessname;
	static public $sign;

    /**
     * init
     * @throws Error
     */
	static function __initialize()
    {
		self::$sign = md5(Config::get('SESSION_CODE'));
		if($domain = Config::get('SESSION_DOMAIN')){
			ini_set('session.cookie_domain',$domain);	
		}
		if($sessname=Config::get('SESSION_NAME')){
			ini_set('session.name',$sessname);	
		}
		$url = ini_get('session.save_path');
		if(!is_dir($url)){
			ini_set('session.save_path',Config::get('APP_ROOT')."/public/session");
			$url = ini_get('session.save_path');
			if(!is_dir($url)){
				throw new \Kuoqi\Error("session[{$url}]存储路径不存在或不可读","debug");
			}
		}
		//强制设置session的path
		isset($_SESSION)||session_start();	
		isset($_SESSION[self::$sign])||$_SESSION[self::$sign] = array();
		//强制key
			
	}

    /**
     * @return string
     * User kuoqi
     * Datetime 2022/2/28 23:02
     * Remark get Session id
     */
	static public function getID(){
		return session_id();	
	}

    /**
     * @param $route
     * @return int
     * User kuoqi
     * Datetime 2022/2/28 23:04
     * Remark 以int类型返回当前数据
     */
	static public function getAsint($route){
		$route =  Format::port2Param('_SESSION.'.self::$sign.'.'.$route);
		$code = null;
		if(eval('if(isset($'.$route.')){$code=$'.$route.';}else{$code=0;}')===false){
			return 0;
		}else{
			return (int)$code;
		}
	}
	
	//设置参数
	static public function set($n,$v){
		$_SESSION[self::$sign][$n] = $v;
	}
	
	static public function get($name)
    {
		$name =  Format::port2Param('_SESSION.'.self::$sign.'.'.$name);
        $code = null;
		if(eval('if(isset($'.$name.')){$code=$'.$name.';}else{$code=null;}')===false){
			return null;
		}else{
			return $code;
		}
		//todo--以观后效
		return isset($_SESSION[self::$sign][$name])?$_SESSION[self::$sign][$name]:NULL;
	}
	
	static public function del($name)
    {
		if(isset($_SESSION[self::$sign][$name]))
		unset($_SESSION[self::$sign][$name]);
		return null;
	}
	
	//destroy
	static public function destroy(){
		isset($_SESSION)&&session_destroy();
	}
}